

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>fetch-post封装</title>
</head>
<body>
    <div class="box"></div>
    <script>
       

       

        function postData(url, data) {
            // Default options are marked with *
            return fetch(url, {
                body: JSON.stringify(data), // must match 'Content-Type' header
                cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
                credentials: 'same-origin', // include, same-origin, *omit
                // headers: {
                // 'user-agent': 'Mozilla/4.0 MDN Example',
                // 'content-type': 'application/json'
                // },
                method: 'POST', // *GET, POST, PUT, DELETE
                mode: 'cors', // no-cors, cors, *same-origin
                // redirect: 'follow', // manual, *follow, error
                // referrer: 'no-referrer', // *client, no-referrer
            })
            .then(response => response.json()) // parses response to JSON
        }

        postData('https://hmajax.itheima.net/api/register', {username: 'ceshi333333', password: "123456"})
            .then(data => console.log(data)) // JSON from `response.json()` call
            .catch(error => console.error(error))






            
    </script>
</body>
</html>